package top.hmtools.jsCss.jsManager;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.PostConstruct;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import top.hmtools.base.CharsetGuessTools;
import top.hmtools.base.StringTools;
import top.hmtools.jsCss.autoConfiguration.JsCssAutoConfiguration;
import top.hmtools.jsCss.common.CommonTools;

/**
 * 缺省的javascript文件管理者
 * @author HyboJ
 * 创建日期：2017-9-26下午9:42:56
 */
@Component
public class JavascriptManagerDefault implements IJavascriptManager{
	
	protected final Logger logger = LoggerFactory.getLogger(JavascriptManagerDefault.class);
	
	@Autowired
	private JsCssAutoConfiguration jsCssAutoConfiguration;
	
	/**
	 * javascript文件缓存库
	 */
	private static Map<String, String> JS_REPERTORY = new HashMap<String, String>();
	
	private List<File> filePaths = new ArrayList<>();
	
	private String encoding = "UTF-8";

	@Override
	@PostConstruct
	public void init() {
		//初始化文件路径集合与文件编码格式，用于刷新
		String jsFilesPathStr = this.jsCssAutoConfiguration.getJsFilesPaths();
		this.logger.info("监控js的磁盘路径有：{}",jsFilesPathStr);
		String[] pathsArr = jsFilesPathStr.split(",");
		List<String> pathList = Arrays.asList(pathsArr);
		for(String path:pathList){
			this.filePaths.add(new File(path));
		}
		
		this.encoding = this.jsCssAutoConfiguration.getEncoding();
		String[] extensions = {"js"}; 
		
		//尝试加载当前运行工程classpath中的javascript文件
		try {
			Enumeration<URL> resources = Thread.currentThread().getContextClassLoader().getResources("");
			CommonTools.loadContent(encoding, resources, JS_REPERTORY,extensions);
		} catch (IOException e1) {
			this.logger.error("尝试从classpath中加载资源失败："+e1.getMessage(),e1);
		}
		
		
		//将指定文件夹中的所有js文件内容读取到缓存
		for(File pathTmp:filePaths){
			try {
				if(!pathTmp.isDirectory()||!pathTmp.exists()){
					continue;
				}
				Collection<File> jsFilesTmp = FileUtils.listFiles(pathTmp, extensions, true);
				for(File fileTmp:jsFilesTmp){
					String fileName = fileTmp.getName();
					//猜测原始文件字符编码
					byte[] byteArray = FileUtils.readFileToByteArray(fileTmp);
					String guessedEncoding = CharsetGuessTools.doGuess(byteArray);
					if(StringTools.isBlank(guessedEncoding)){
						guessedEncoding=this.encoding;
					}
					String fileContent = FileUtils.readFileToString(fileTmp, guessedEncoding);
					this.logger.debug("猜测文件{}的字符编码为：{}，内容摘要有：{}",fileName,guessedEncoding,(StringTools.isBlank(fileContent)?"":fileContent.length()<20?fileContent:fileContent.substring(0, 20)+"..."));
					JS_REPERTORY.put(fileName.toLowerCase(), fileContent);
				}
			} catch (Exception e) {
				logger.error("获取"+pathTmp.getAbsolutePath()+"目录下的js文件失败"+e.getMessage(),e);
			}
		}
		this.logger.info("当前成功加载javascript文件总条数是：{}",JS_REPERTORY.size());
	}

	@Override
	public void destory() {
		if(JS_REPERTORY != null){
			JS_REPERTORY.clear();
		}
	}

	@Override
	public String getJs(String jsNames) {
		if(jsNames != null && !"".equals(jsNames)){
			String[] jsNamesArr = jsNames.split(",");
			return this.getJs(jsNamesArr);
		}else{
			return ";";
		}
	}

	@Override
	public String getJs(List<String> jsNames) {
		if(jsNames != null && jsNames.size()>0){
			String[] jsNamesArr = jsNames.toArray(new String[0]);
			return this.getJs(jsNamesArr);
		}else{
			return ";";
		}
	}

	@Override
	public String getJs(String[] jsNames) {
		StringBuffer sb_result = new StringBuffer(";");
		if(jsNames != null && jsNames.length>0){
			for(String fileName:jsNames){
				String js_content_tmp = JS_REPERTORY.get(fileName.trim().toLowerCase());
				if(js_content_tmp == null || "".equals(js_content_tmp)){
					continue;
				}
				sb_result.append(js_content_tmp+";");
			}
			return sb_result.toString();
		}else{
			return ";";
		}
	}

	@Override
	public boolean refresh() {
		boolean result = false;
		try {
			this.destory();
			this.init();
			result=true;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return result;
	}

	@Override
	public List<String> listJsFilenames() {
		List<String> result = new ArrayList<String>();
		if(JS_REPERTORY != null){
			Set<String> keySet = JS_REPERTORY.keySet();
			result.addAll(keySet);
			Collections.sort(result);
		}
		return result;
	}

}
